package com.qf.commerce.controller;

import com.github.pagehelper.PageInfo;
import com.qf.commerce.common.dto.FunctionDto;
import com.qf.commerce.common.vo.FunctionVo;
import com.qf.commerce.entity.Function;
import com.qf.commerce.resp.ResponseResult;
import com.qf.commerce.service.FunctionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/function")
public class FunctionController {
    @Autowired
    private FunctionService functionService;

    @PostMapping("/add")
    public ResponseResult<Integer> add(@RequestBody FunctionDto functionDto){
        try {
            return ResponseResult.success(functionService.insertFunction(functionDto));
        } catch (Exception e) {
            return ResponseResult.fail();
        }
    }

    @DeleteMapping("/delete")
    public ResponseResult<Integer> add(@RequestParam List<Long> ids){
        try {
            return ResponseResult.success(functionService.deleteFunction(ids));
        } catch (Exception e) {
            return ResponseResult.fail();
        }
    }

    @PutMapping("/update")
    public ResponseResult<Integer> update(@RequestBody Function function){
        try {
            return ResponseResult.success(functionService.updateFunction(function, function.getFunctionId()));
        } catch (Exception e) {
            return ResponseResult.fail();
        }
    }



    @GetMapping("listDetail")
    public ResponseResult<Function> listDetail(@RequestParam Long functionId){
        try {
            return ResponseResult.success(functionService.selectFunctionDetail(functionId));
        } catch (Exception e) {
            return ResponseResult.fail();
        }

    }
    @GetMapping("list")
    public ResponseResult<PageInfo<FunctionVo>> list(Integer status, String functionName, String functionCoding, Integer page, Integer size){
        try {
            return ResponseResult.success(functionService.selectFunction(status,functionName,functionCoding,page,size));
        } catch (Exception e) {
            return ResponseResult.fail();
        }

    }
    @GetMapping("listByTime")
    public ResponseResult<PageInfo<FunctionVo>> listByTime(String startCreateTime, String endCreateTime, Integer page, Integer size){
        try {
            return ResponseResult.success(functionService.selectFunctionByTime(startCreateTime,endCreateTime,page,size));
        } catch (Exception e) {
            return ResponseResult.fail();
        }

    }
}
